package com.zxmc.common.utils;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
public class BloomFilterUtil {
    private static final int EXPECTED_INSERTIONS = 100000;
    private static final double FALSE_POSITIVE_PROBABILITY = 0.01;

    private final BloomFilter<CharSequence> bloomFilter;

    public BloomFilterUtil() {
        this.bloomFilter = BloomFilter.create(Funnels.stringFunnel(StandardCharsets.UTF_8), EXPECTED_INSERTIONS, FALSE_POSITIVE_PROBABILITY);
    }

    public void add(String key) {
        bloomFilter.put(key);
    }

    public boolean mightContain(String key) {
        return bloomFilter.mightContain(key);
    }
}
